package com.biejiaogroup.librarymanager.controller;

import com.biejiaogroup.librarymanager.entity.User;
import com.biejiaogroup.librarymanager.service.UserService;
import com.biejiaogroup.librarymanager.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Vector;

@WebServlet("/queryUser")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Vector<User> v = userService.getAll();
        request.getSession().setAttribute("users",v);
        request.getRequestDispatcher("userManager.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String userName = request.getParameter("userName");
        if(StringUtil.isEmpty(userName)) doGet(request,response);
        else
        {
            Vector<User> v = userService.getAllUserByName(userName);
            request.getSession().setAttribute("users",v);
            request.getRequestDispatcher("userManager.jsp").forward(request,response);
        }

    }
}
